﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QD.Tools
{
    public static class ControlHelper
    {
        /// <summary>
        /// 在TextBox光标处插入内容
        /// </summary>
        /// <param name="control"></param>
        /// <param name="content"></param>
        public static void InsertActiveText(this TextBox control, string content)
        {
            string s = control.Text;
            int idx = control.SelectionStart;
            s = s.Insert(idx, content);

            control.Text = s;
            control.SelectionStart = idx + (content.Length);
            control.Focus();
        }

        /// <summary>
        /// 设置子控只读状态
        /// </summary>
        /// <param name="control"></param>
        public static void SetChildReadonly(Control control, bool isReadonly, List<Control> ignoreControls)
        {
            SetChildReadonly(control, isReadonly, ignoreControls.ToArray());
        }

        /// <summary>
        /// 设置子控只读状态
        /// </summary>
        /// <param name="control"></param>
        public static void SetChildReadonly(Control control, bool isReadonly, params Control[] ignoreControls)
        {
            if (control.HasChildren)
            {
                foreach (Control childControl in control.Controls)
                {
                    if (!ignoreControls.Contains(childControl))
                    {
                        if (childControl is TextBox)
                        {
                            ((TextBox)childControl).ReadOnly = isReadonly;
                        }
                        else if (childControl is RichTextBox)
                        {
                            ((RichTextBox)childControl).ReadOnly = isReadonly;
                        }
                        else if (childControl is DataGridView)
                        {
                            ((DataGridView)childControl).ReadOnly = isReadonly;
                            continue;
                        }
                        else if (childControl is TreeView)
                        {
                            ((TreeView)childControl).Enabled = !isReadonly;
                        }
                        else if (childControl is ComboBox)
                        {
                            ((ComboBox)childControl).Enabled = !isReadonly;
                        }
                        else if (childControl is CheckBox)
                        {
                            ((CheckBox)childControl).Enabled = !isReadonly;
                        }
                        else if (childControl is Button)
                        {
                            ((Button)childControl).Enabled = !isReadonly;
                        }
                        else if (childControl is ICSharpCode.TextEditor.TextEditorControl)
                        {
                            ((ICSharpCode.TextEditor.TextEditorControl)childControl).IsReadOnly = isReadonly;
                        }
                        else if (childControl is NumericUpDown)
                        {
                            ((NumericUpDown)childControl).Enabled = !isReadonly;
                        }

                        SetChildReadonly(childControl, isReadonly, ignoreControls);
                    }
                }
            }
        }

        /// <summary>
        /// 设置子控启用状态
        /// </summary>
        /// <param name="control"></param>
        public static void SetChildEnable(Control control, bool isEnable, params Control[] ignoreControls)
        {
            if (control.HasChildren)
            {
                foreach (Control childControl in control.Controls)
                {
                    if (!ignoreControls.Contains(childControl))
                    {
                        childControl.Enabled = isEnable;
                        SetChildEnable(childControl, isEnable, ignoreControls);
                    }
                }
            }
        }

        /// <summary>
        /// 清空子控件的值
        /// </summary>
        /// <param name="control"></param>
        public static void ClearChildValue(Control control, params Control[] ignoreControls)
        {
            if (control.HasChildren)
            {
                foreach (Control childControl in control.Controls)
                {
                    if (!ignoreControls.Contains(childControl))
                    {
                        if (childControl is TextBox)
                        {
                            ((TextBox)childControl).Clear();
                        }
                        else if (childControl is RichTextBox)
                        {
                            ((RichTextBox)childControl).Clear();
                        }
                        else if (childControl is ComboBox)
                        {
                            ((ComboBox)childControl).SelectedIndex = -1;
                        }
                        else if (childControl is CheckBox)
                        {
                            ((CheckBox)childControl).Checked = false;
                        }
                        else if (childControl is DataGridView)
                        {
                            DataGridView grid = ((DataGridView)childControl);
                            DataTable dt = (DataTable)grid.DataSource;
                            if (dt != null)
                            {
                                dt.Rows.Clear();
                                dt.AcceptChanges();
                                grid.DataSource = dt;
                            }
                        }
                        else if (childControl is ICSharpCode.TextEditor.TextEditorControl)
                        {
                            ((ICSharpCode.TextEditor.TextEditorControl)childControl).Text = string.Empty;
                        }
                        ClearChildValue(childControl, ignoreControls);
                    }
                }
            }
        }
    }
}
